package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.family.LoanNewAdvertCreditCard;
import com.tuanzi.loan.business.entity.family.LoanNewCreditCardInfo;
import com.tuanzi.loan.business.service.family.LoanNewAdvertCreditCardService;
import com.tuanzi.loan.business.service.family.LoanNewCreditCardInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.bean.CurrentUserBean;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 15:54 2018/9/5
 * Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanNewAdvertCreditCardInfoBean extends JSFInfoBean<LoanNewAdvertCreditCard>{
    private static final long serialVersionUID = -8386807959452160714L;

    @Autowired
    private LoanNewAdvertCreditCardService loanNewAdvertCreditCardService;
    @Autowired
    CurrentUserBean currentUserBean;
    @Autowired
    private LoanNewCreditCardInfoService loanNewCreditCardInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private List<LoanNewCreditCardInfo> creditCardInfoList;
    private String[] productArray;

    @PostConstruct
    public void init(){
        String idStr= getParamFromRequest("id");
        creditCardInfoList = loanNewCreditCardInfoService.findAll();

        if(StringUtils.isBlank(idStr)){
            $ = new LoanNewAdvertCreditCard();
            $.setStatus(0);
            changeStatusToEdit();
            return;
        }

        long id = NumberUtils.toLong(idStr);
        $ = loanNewAdvertCreditCardService.findOne(id);
        if(Objects.isNull($)){
            forceRedirect(currentUserBean.getBack());
            return;
        }

        initProperties($);
    }

    private void initProperties(LoanNewAdvertCreditCard loanNewAdvertCreditCard){
        String prdid = loanNewAdvertCreditCard.getPrdid();
        if(StringUtils.isNotBlank(prdid)){
            productArray = StringUtils.split(prdid, ",");
        }
    }


    public void saveInfo(){
        if(productArray !=null && productArray.length >0){
            String prdid = StringUtils.join(productArray, ",");
            $.setPrdid(prdid);
        }
        this.$ = loanNewAdvertCreditCardService.saveOrUpdate($);
        changeStatusToView();
        stringRedisTemplate.delete(LoanRedisKey.LOAN_NEW_CREDIT_CARD_LIST);
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.NEW_CREDIT_CARD_ADVERT);
    }
}
